/*
 * TreeDemo.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.cute4j.swing.Component;
import com.cute4j.swing.Tree;
import com.cute4j.swing.tree.DefaultMutableTreeNode;
import com.cute4j.swing.tree.DefaultTreeModel;

/**
 * Demonstration of the Tree class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class TreeDemo extends DemoModule {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new TreeDemo object.
   */
  public TreeDemo() {
    super("Tree Demo", "Demonstration of the Tree class.");
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected Component buildDemoComponent() {
    return new Tree(buildTreeModel());
  }

// Private Methods. ------------------------------------------------------------

  private DefaultTreeModel buildTreeModel() {
    BufferedReader reader = null;
    try {
      DefaultMutableTreeNode top = new DefaultMutableTreeNode("Music");
      DefaultMutableTreeNode catagory = null;
      DefaultMutableTreeNode artist = null;
      DefaultMutableTreeNode record = null;
      InputStream is = getClass().getResourceAsStream("resources/Tree.txt");
      InputStreamReader isr = new InputStreamReader(is);
      reader = new BufferedReader(isr);
      String line = reader.readLine();
      while (line != null) {
        char linetype = line.charAt(0);
        switch (linetype) {
          case 'C':
            catagory = new DefaultMutableTreeNode(line.substring(2));
            top.add(catagory);
            break;
          case 'A':
            if (catagory != null) {
              artist = new DefaultMutableTreeNode(line.substring(2));
              catagory.add(artist);
            }
            break;
          case 'R':
            if (artist != null) {
              record = new DefaultMutableTreeNode(line.substring(2));
              artist.add(record);
            }
            break;
          case 'S':
            if (record != null) {
              record.add(new DefaultMutableTreeNode(line.substring(2)));
            }
            break;
          default:
            break;
        }
        line = reader.readLine();
      }
      return new DefaultTreeModel(top);
    } catch (IOException e) {
      return new DefaultTreeModel(new DefaultMutableTreeNode("Invalid Tree"));
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException ioe) {
        }
      }
    }
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts demo as standalone application.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    new TreeDemo().show();
  }

}
